var submit = 2
layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 调度单表管理
     */
    var TDispatch = {
        tableId: "tDispatchTable"
    };
    
    /**
     * 初始化表格的列
     */
    TDispatch.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键id'},
            {width: 180,align: 'center', toolbar: '#tableBar', title: '操作'},
            {field: 'tstate', sort: true, title: '状态', style:'font-weight: 600;'},
            {field: 'startDate', sort: true, title: '发车日期', style:'font-weight: 600;'},
            {field: 'dispatchNumber', sort: true, title: '运输单号', style:'color: #2b85e4;font-weight: 600;',width: 180},
            {field: 'carnumber', sort: true, title: '车牌号', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'siji', sort: true, title: '司机', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'sijidianhua', sort: true, title: '联系方式', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'chexing', sort: true, title: '车型', style:'background-color: #dbf1e1;font-weight: 600;'},
            {field: 'mileageTraveled', sort: true, title: '行驶里程', style:'font-weight: 600;', templet: '#mileageTraveledTpl'},
            {field: 'loadingType', sort: true, title: '装车方', style:'font-weight: 600;'},
            {field: 'loadingPersonnel', title: '<span style="color: #2b85e4;"><i class="layui-icon">&#xe716;</i> 装车人员</span>', style:'color: #2b85e4;', edit:'text'},

            {field: 'zhunzailiang', sort: true, title: '准载体积m3', style:'font-weight: 600;'},
            {field: 'fulllv', sort: true, title: '满载率%', style:'font-weight: 600;', totalRowText: '合计：'},
            {field: 'toovolume', sort: true, title: '总体积m3', style:'font-weight: 600;', totalRow: true},
            {field: 'tooNum', sort: true, title: '总数量', style:'font-weight: 600;', totalRow: true},
            {field: 'toomoney', sort: true, title: '货值', style:'font-weight: 600;', totalRow: true},
            {field: 'carrierName', sort: true, title: '承运商', style:'font-weight: 600;'},
            {field: 'lineName', sort: true, title: '线路', style:'font-weight: 600;'},
            {field: 'consultMoney', sort: true, title: '系统参考价￥', style:'font-weight: 600;',templet: '#consultMoneyTpl', totalRow: true},
            {field: 'freightMoney', sort: true, title: '运费', style:'font-weight: 600;', totalRow: true},
            {field: 'xiechefei', sort: true, title: '卸车费', style:'font-weight: 600;', totalRow: true},
            {field: 'createTime', sort: true, title: '调度时间', style:'font-weight: 600;'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    TDispatch.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['carnumber'] = $("#carnumber").val();
        queryData['driver1'] = $("#driver1").val();
        table.reload(TDispatch.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TDispatch.tableId,
        url: Feng.ctxPath + '/tDispatch/list_examine',
        page: true,
        limit: 40,
        height: "full-60",
        totalRow: true,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        cellMinWidth: 100,
        cols: TDispatch.initColumn()
    });

    // 编辑单元格 事件
    table.on('edit(' + TDispatch.tableId + ')', function (obj) {
        var data = obj.data;//得到所在行所有键值
		var value = obj.value;//修改后的值
        var field = obj.field;
		if(field == 'loadingPersonnel'){
	        var ajax = new $ax(Feng.ctxPath + "/orderTransportDispatch/edititem?id="+data.id+"&loadingPersonnel="+value, function (data) {
	    		if (data.success) {
	                Feng.success("修改成功!");
				} else {
	                Feng.error("修改失败！"+data.message);
				}
	        }, function (data) {
	            Feng.error("修改失败!" + data.responseJSON.message + "!");
	        });
	        ajax.start();
		}
    });

     // 批量审核
     $('#btnSubmission').click(function () {
    	var checkStatus = table.checkStatus('tDispatchTable');
        data = checkStatus.data;
        if (data.length>0) {
            var strings = [];
            for (var i = 0; i < data.length; i++) {
            	strings.push(data[i].dispatchNumber);
			}
			
            layer.confirm('确定通过审核吗?', function(index){
            	layer.close(index);
            	var msg;
                $.ajax({
                	url:Feng.ctxPath + "/orderTransportDispatch/tongguo?strings="+strings,
                	cache: false,
                	dataType:"json",
                	beforeSend:function(){
                		msg = layer.msg('提交中。。。请耐心等待', {
                			  icon: 16,
                			  shade: [0.2,'#000'], //0.1透明度的白色背景
                			  time: false
                		});
                	},
                	success:function(data){
                    	layer.close(msg); 
                		if (data.success) {
							if (data.message !=null &&data.message!='') {
			    				var msg2 =  layer.open({
				  					  title: '提示' ,
				  					  content: data.message,
				  					  btn: ['确定'],
				  					  yes: function (){
				  						  	layer.close(msg2);
	            							table.reload(TDispatch.tableId);
				  					  }
			    				}); 
							} else {
				            	Feng.success("提交成功!");
	            				table.reload(TDispatch.tableId);
							}
						} else {
  		                    Feng.error("提交失败！"+data.message);
						}
                	},
                	error:function(data){
                    	layer.close(msg); 
                        Feng.error("提交失败！" + data.responseJSON.message)
                	}
                })
            }); 
		} else {
			layer.alert('请至少选中一条数据');
		}
     });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TDispatch.search();
    });

    // 工具条点击事件
    table.on('tool(' + TDispatch.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'tooCarsy') {
            layer.open({
                type: 2,
                title: '查询配置明细',
                area: ['900px', '480px'],
                anim: 5,
                content: Feng.ctxPath + '/orderTransportDispatch/tochecktooy?dispatchNumber=' + data.dispatchNumber,
            });
        }  else if (layEvent === 'tooConsultMoneyDetailed') {
            layer.open({
                type: 2,
                title: '费用信息',
                area: ['1100px', '500px'],
                anim: 5,
                content: Feng.ctxPath + '/transportCost?dispatchNumber=' + data.dispatchNumber,
            });
        } else if (layEvent === 'bohui') {
			submit = 2
            layer.open({
                type: 2,
                title: '驳回',
                area: ['1000px', '550px'],
                anim: 5,
                content: Feng.ctxPath + '/orderTransportDispatch/edit_examine?dispatchNumber=' + data.dispatchNumber,
	            end: function () {
					if(submit == 1){
	            		table.reload(TDispatch.tableId);
					}
	            }
            });
        } else if (layEvent === 'count') {
			submit = 2
	        layer.open({
	            type: 2,
	            title: '计算里程',
	            area: ['900px', '600px'],
	            anim: 5,
	            content: Feng.ctxPath + '/transportReceipt/toCount?dispatchNumber='+data.dispatchNumber,
	            end: function () {
					if(submit == 1){
						if(TDispatchInfoDlg.data.mileageTraveled != "" && TDispatchInfoDlg.data.mileageTraveled != null){
			                var ajax = new $ax(Feng.ctxPath + "/orderTransportDispatch/edititem?id="+data.id+"&mileageTraveled="+TDispatchInfoDlg.data.mileageTraveled, function (data) {
			            		if (data.success) {
				                    Feng.success("修改成功!");
		            				table.reload(TDispatch.tableId);
								} else {
				                    Feng.error("修改失败！"+data.message);
								}
			                }, function (data) {
			                    Feng.error("确认失败!" + data.responseJSON.message + "!");
			                });
			                ajax.start();
						}
					}
	            }
	        });
        }
    });

    // 单击行事件
    table.on('row(' + TDispatch.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + TDispatch.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + TDispatch.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });
    
});
